import { defineStore } from 'pinia'
import { local_Storage, session_Storage } from '@/ts/enum'
import { LoginService, MenuService } from '@/api'
import { MenuList, Staff } from '@/ts/type'

type userTokenType = {
	token: string,
	tokenHeader: string
}

type useStaffType = {
	info: Staff
}
type useMenuType = {
	list: MenuList[]
	isCollapse: boolean
	active: string
	openeds?: string[]
}
export const useToken = defineStore(local_Storage.token, {
  state: (): userTokenType => ({
    token: '',
    tokenHeader: ''
  }),
  persist: { storage: localStorage }
})
export const useStaff = defineStore(local_Storage.staff, {
  state: (): useStaffType => ({ info: {} }),
  actions: {
    async getInfo() {
      await LoginService.info().then(async res => {
        const { data } = res
        this.info = data
        await useMenu().getMenuList()
      })
    },
    changePicture(pic: string) {
      this.info.picture = pic
    }
  },
  persist: { storage: localStorage }
})

export const useMenu = defineStore(session_Storage.menu, {
  state: (): useMenuType => ({
    list: [],
    isCollapse: false,
    active: '/base',
    openeds: []
  }),
  actions: {
    async getMenuList() {
      await MenuService.menuList().then(res => {
        const { data } = res
        this.list = data
      })
    },
    changeCollapse(): void {
      this.isCollapse = !this.isCollapse
    },
    changeActive(str: string) {
      this.active = str
    },
    changeOpeneds(str: string[]) {
      this.openeds = str
    }
  },
  persist: { storage: sessionStorage }
})

export const logout_one = (): void => {
  useToken().$reset()
}

export const logout_two = (): void => {
  useMenu().$reset()
  useStaff().$reset()
  localStorage.clear()
  sessionStorage.clear()
}